<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
    </ul>
    <script>
        var list = document.getElementsByTagName('li');
        for(var i=0;i<list.length;i++){
            list[i].index = i+1;
            // list[i].setAttribute('index',i);
            list[i].onclick = function(){
                // console.log(i)
                console.log(this.index);
                // console.log(this.getAttribute('index'));
            }
        }
    </script>
</body>
</html>